import re
from collections import defaultdict

def split_text_by_headers(text):
    # 定义标题模式
    header_patterns = [
        r'#\s+\d+\s+(.+)',  # 一级标题
        r'#\s+\d+\.\d+\s+(.+)',  # 二级标题
        r'#\s+\d+\.\d+\.\d+\s+(.+)',  # 三级标题
        r'#\s+\d+\.\d+\.\d+\.\d+\s+(.+)',  # 四级标题
    ]

    # 合并所有标题模式
    combined_pattern = '|'.join(header_patterns)

    # 使用正则表达式找到所有标题及其位置
    headers = []
    for m in re.finditer(combined_pattern, text):
        # 提取标题内容
        header_text = m.group(0)
        # 提取编号部分
        number_part = header_text.split(' ', 2)[1]
        #提取标题内容
        header_content = header_text.split(' ', 2)[2]
        # 计算层级（通过统计编号中的点号数量）
        level = number_part.count('.') + 1
        headers.append((m.start(), header_content, level))

    # 创建一部字典来存储标题和对应的文本内容，指定默认值类型为 str
    sections = defaultdict(str)
    last_header = None

    start = 0
    for header in headers:
        end = header[0]
        if last_header:
            sections[last_header] += text[start:end].strip() + '\n'
        start = end
        last_header = (header[1], header[2])  # 标题和层级

    # 添加最后一部分
    if last_header:
        sections[last_header] += text[start:].strip()

    return sections

def clean_text(text):
    # 去除多余的空格和换行符
    text = re.sub(r'\s+', '', text)
    return text

if __name__ == '__main__':
    # 示例文本
    txt_path = "tmp_txt/02_clean/02_银河麒麟桌面操作系统V10 SP1 2503常见问题(FAQ).txt"
    with open(txt_path, "r", encoding="utf-8") as doc:
        text = doc.read()

    # 指定输出的文件路径
    output_header_path = "tmp_txt/03_split/header/02_header_银河麒麟桌面操作系统V10 SP1 2503常见问题(FAQ).txt"

    # 指定输出的文件路径
    output_path = "tmp_txt/03_split/02_银河麒麟桌面操作系统V10 SP1 2503常见问题(FAQ).txt"

    # 分割文本
    sections = split_text_by_headers(text)

    #保存分割文本（header）
    with open(output_header_path, "w", encoding="utf-8") as output_file:
        for headers, content in sections.items():
            header = clean_text(headers[0])
            output_file.write(header + "\n")

    print(f"分割后的标题已成功保存到 {output_header_path}")

    print("\n-------------------------------------------------------\n")

    #保存分割文本（整体）
    with open(output_path, "w", encoding="utf-8") as output_file:
        for header, content in sections.items():
            content = clean_text(content)
            output_file.write(content + "\n")

    print(f"分割后的文本已成功保存到 {output_path}")